package org.jarraiders.musiclibrary.server;

import java.util.ArrayList;

import org.jarraiders.musiclibrary.client.TracksService;
import org.jarraiders.musiclibrary.domain.Track;
import org.jarraiders.musiclibrary.server.service.ConverterFabric;
import org.jarraiders.musiclibrary.server.service.TrackServiceDAO;
import org.jarraiders.musiclibrary.shared.domain.TrackDTO;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class TracksServiceImpl extends RemoteServiceServlet implements TracksService {

	private static TrackServiceDAO service;
	
	static {
		service = new TrackServiceDAO();
	}
	@Override
	public TrackDTO addTrack(TrackDTO track) {
		Track trackP= ConverterFabric.getTrackDAO(track);
		service.addNewEntity(trackP);
		return ConverterFabric.getTrackDTO(service.getEntityById(track.getTrackId()));
	}

	@Override
	public boolean deleteTrack(String id) {
		service.deleteEntity(service.getEntityById(id));
		return true;
	}

	@Override
	public TrackDTO getTrack(String id) {
		
		return ConverterFabric.getTrackDTO(service.getEntityById(id));
	}

	@Override
	public ArrayList<TrackDTO> getTrackDetails() {
		ArrayList<Track> list =(ArrayList<Track>) service.getAllEntities();
		ArrayList<TrackDTO> listDTO = new ArrayList<TrackDTO>();
		for (Track track : list) {
			listDTO.add(ConverterFabric.getTrackDTO(track));
		}
		return listDTO;
	}

	@Override
	public TrackDTO updateTrack(TrackDTO track) {
		Track trackP=ConverterFabric.getTrackDAO(track);
		service.updateEntity(trackP);
		return ConverterFabric.getTrackDTO(service.getEntityById(trackP.getTrackId()));
	}

	@Override
	public ArrayList<TrackDTO> searchTrack(String searchText) {
		ArrayList <Track> list = (ArrayList<Track>) service.searchEntity(searchText); 
		ArrayList<TrackDTO> listDTO = new ArrayList<TrackDTO>();
		for (Track track : list) {
			listDTO.add(ConverterFabric.getTrackDTO(track));
		}
		return listDTO;
	}

}
